import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:cher_flare/config/config.dart';
import 'package:cher_flare/modules/login/models/ly_login_info_manager.dart';
import 'package:cher_flare/modules/login/models/ly_login_model.dart';
import 'package:cher_flare/utils/app_color.dart';
import 'package:flutter/material.dart';

import '../../common/models/ly_user_works_model.dart';

class LYDiscoveryCardItem extends StatefulWidget {
  final bool isGreat;
  final String? content;
  final String? title;
  final String? image;
  final String? nickName;
  final String? avatar;
  final String topIcon;
  final bool? isSave;
  final Function()? onTap;
  final Function()? topIconOnTap;
  final Function()? greatOnTap;
  final Function()? avatarOnTap;

  const LYDiscoveryCardItem(
      {super.key,
      this.isGreat = false,
      this.content,
      this.title,
      this.image,
      this.nickName,
      this.avatar,
      this.onTap,
      this.topIcon = 'more_icon.png',
      this.isSave,
      this.topIconOnTap,
      this.greatOnTap,
      this.avatarOnTap});

  @override
  State<LYDiscoveryCardItem> createState() => _LYDiscoveryCardItemState();
}

class _LYDiscoveryCardItemState extends State<LYDiscoveryCardItem> {
  @override
  Widget build(BuildContext context) {
    final String avatar;
    if (widget.isSave == true) {
      avatar =
          LYLoginInfoManager.getInstance.loginModel?.userInfo?.avatarThumbUrl ??
              '';
    } else {
      avatar = widget.avatar ?? '';
    }
    return InkWell(
      onTap: widget.onTap,
      child: Container(
        height: 320,
        width: double.infinity,
        decoration: BoxDecoration(
          gradient: const LinearGradient(
              colors: [AppColors.colorF3FFF5, AppColors.colorF4FFDC]),
          borderRadius: BorderRadius.circular(12),
        ),
        child: Stack(
          fit: StackFit.expand,
          children: [
            Positioned.fill(
                child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(12),
                  child: widget.isSave == true
                      ? Image.file(
                          File(
                              "${Config.applicationDirPath}/${widget.image ?? ''}"),
                          height: 190,
                          fit: BoxFit.cover,
                        )
                      : Image.asset(
                          'assets/images/works_images/${widget.image}',
                          height: 190,
                          fit: BoxFit.cover,
                        ),
                ),
                Padding(
                  padding: const EdgeInsets.only(
                      left: 10, top: 10, right: 10, bottom: 10),
                  child: Text(
                    widget.title ?? '',
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: const TextStyle(
                        fontSize: 16, color: AppColors.color202020),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.only(
                    left: 10,
                    right: 10,
                  ),
                  child: Text(
                    widget.content ?? '',
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                        fontSize: 12,
                        color: colorWithRGBA(r: 0, g: 0, b: 0, a: 0.8)),
                  ),
                ),
                Expanded(
                  child: Container(
                    padding: const EdgeInsets.symmetric(horizontal: 10),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.center,
                      children: [
                        ClipRRect(
                          borderRadius: BorderRadius.circular(20),
                          child: InkWell(
                            onTap: widget.avatarOnTap,
                            child: CachedNetworkImage(
                              imageUrl: avatar,
                              width: 40,
                              height: 40,
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                        const SizedBox(
                          width: 10,
                        ),
                        Expanded(
                          child: Text(
                            widget.nickName ?? '',
                            overflow: TextOverflow.ellipsis,
                            maxLines: 1,
                            style: const TextStyle(
                                color: AppColors.color202020, fontSize: 16),
                          ),
                        ),
                        InkWell(
                          onTap: widget.greatOnTap,
                          child: Image.asset(
                            'assets/images/great_icon_${widget.isGreat ? "h" : "n"}.png',
                            width: 34,
                            height: 34,
                          ),
                        )
                      ],
                    ),
                  ),
                ),
              ],
            )),
            Visibility(
              visible: widget.topIconOnTap != null,
              child: Positioned(
                top: 12,
                right: 12,
                child: InkWell(
                  onTap: widget.topIconOnTap,
                  child: Image.asset(
                    'assets/images/${widget.topIcon}',
                    width: 28,
                    height: 28,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
